package com.atguigu.gmall.product.service.impl;
import com.atguigu.gmall.model.product.BaseCategoryTrademark;
import com.atguigu.gmall.model.product.BaseTrademark;
import com.atguigu.gmall.model.product.CategoryTrademarkVo;
import com.atguigu.gmall.product.mapper.BaseCategoryTrademarkMapper;
import com.atguigu.gmall.product.mapper.BaseTrademarkMapper;
import com.atguigu.gmall.product.service.BaseCategoryTrademarkService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
@SuppressWarnings("all")
public class BaseCategoryTrademarkServiceImpl extends ServiceImpl<BaseCategoryTrademarkMapper,BaseCategoryTrademark> implements BaseCategoryTrademarkService {
    @Autowired
    private BaseCategoryTrademarkMapper baseCategoryTrademarkMapper;
    @Autowired
    private BaseTrademarkMapper baseTrademarkMapper;

    //根据category3Id获取品牌列表
    @Override
    public List<BaseTrademark> findTrademarkList(Long category3Id) {
        //先根据category3Id查询出BaseCategoryTrademark表的数据
        QueryWrapper<BaseCategoryTrademark> category3_id = new QueryWrapper<BaseCategoryTrademark>().eq("category3_id", category3Id);
        List<BaseCategoryTrademark> baseCategoryTrademarks = baseCategoryTrademarkMapper.selectList(category3_id);

        if(!CollectionUtils.isEmpty(baseCategoryTrademarks)){
            //如果不为空,取出集合中的trademark_id值组成一个集合,然后在去查询base_trademark表
            List<Long> tradeMarkIdList = baseCategoryTrademarks.stream().map(baseCategoryTrademark -> {
                return baseCategoryTrademark.getTrademarkId();
            }).collect(Collectors.toList());
            return baseTrademarkMapper.selectBatchIds(tradeMarkIdList);
        }
        //如果集合为空则默认返回为空
        return null;
    }

    //根据category3Id获取可选品牌列表
    @Override
    public List<BaseTrademark> findCurrentTrademarkList(Long category3Id) {
        QueryWrapper<BaseCategoryTrademark> category3_id = new QueryWrapper<BaseCategoryTrademark>().eq("category3_id", category3Id);
        List<BaseCategoryTrademark> baseCategoryTrademarkList = baseCategoryTrademarkMapper.selectList(category3_id);

        QueryWrapper<BaseTrademark> idQuery = new QueryWrapper<BaseTrademark>();

        if(!CollectionUtils.isEmpty(baseCategoryTrademarkList)){
            List<Long> trademarkId = baseCategoryTrademarkList.stream().map(baseCategoryTrademark -> {
                return baseCategoryTrademark.getTrademarkId();
            }).collect(Collectors.toList());

            idQuery.notIn("id", trademarkId);
            List<BaseTrademark> trademarkList = baseTrademarkMapper.selectList(idQuery);
            return trademarkList;
        }
        return null;
    }

    //保存分类品牌关联
    @Override
    public void save(CategoryTrademarkVo categoryTrademarkVo) {
        List<Long> trademarkIdList = categoryTrademarkVo.getTrademarkIdList();

        List<BaseCategoryTrademark> baseCategoryTrademarkCollect = trademarkIdList.stream().map(trademarkId -> {
            BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
            baseCategoryTrademark.setCategory3Id(categoryTrademarkVo.getCategory3Id());
            baseCategoryTrademark.setTrademarkId(trademarkId);
            return baseCategoryTrademark;
        }).collect(Collectors.toList());
        this.saveBatch(baseCategoryTrademarkCollect);
    }



}
